package pierwszy;

import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class MainPage {
	private WebDriver driver;
	private WebDriverWait wait;
	
	private WebElement ksPortal;
	private WebElement ksDotacje;	
	private WebElement ksKsiegowosc;	
	
	MainPage(WebDriver driver, WebDriverWait wait){
		this.driver = driver;
		this.wait = wait;
		
		if(!driver.getTitle().equals("multiCMS - Wybór serwisu")){
			throw new NoSuchWindowException("This is not the Main page");
		}	
	}
	
	public LogInPage LogOut(){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.LogOutButton));
		WebElement lgo = driver.findElement(UiMapping.LogOutButton);
		lgo.click();
		
		if (!driver.getTitle().equals("mAdmin- LOGOWANIE DO SYSTEMU")){
			throw new NoSuchWindowException("This is not the Login page");
		}
		
		return new LogInPage(driver, wait);
	}
	
	public KsPortalPage chooseSite(String site){
		WebElement ksPortal = driver.findElement(UiMapping.passSite(site));	
		ksPortal.click();
		
		ksPortal.submit();
		
		return new KsPortalPage(driver, wait);
	}
	
	//select Ks
	public MainPage selectKsPortal(){
		ksPortal = driver.findElement(MainPageUiMapping.ksPortalRBtn);
		ksPortal.click();

		return this;
	}
	
	public MainPage selectKsDotacje(){
		ksDotacje = driver.findElement(MainPageUiMapping.ksDotacjeRBtn);
		ksDotacje.click();
		
		return this;
	}
	
	public MainPage selectKsKsiegowosc(){	
		ksKsiegowosc = driver.findElement(MainPageUiMapping.ksKsiegowoscRBtn);
		ksKsiegowosc.click();
		
		return this;
	}
	
	//submit Ks
	public MainPage submitKsFailure(){
		WebElement wybieram = driver.findElement(MainPageUiMapping.wybieramBtn);
		wybieram.submit();
		
		return this;
	}
	
	public KsPPage submitKsPortal(){
		WebElement wybieram;
		
		selectKsPortal();
		
		wybieram = driver.findElement(MainPageUiMapping.wybieramBtn);
		wybieram.submit();
		
		return new KsPPage(driver, wait);
	}
	
	public KsDPage submitKsDotacje(){
		WebElement wybieram; 
		selectKsDotacje();
		
		wybieram = driver.findElement(MainPageUiMapping.wybieramBtn);
		wybieram.submit();
		
		return new KsDPage(driver, wait);
	}
	
	public KsKPage submitKsKsiegowosc(){
		WebElement wybieram; 
		
		selectKsKsiegowosc();
		
		wybieram = driver.findElement(MainPageUiMapping.wybieramBtn);
		wybieram.submit();
		
		return new KsKPage(driver, wait);
	}
	
	//getters
	public boolean getStateOfKsPortalRBtn(){
		ksKsiegowosc = driver.findElement(MainPageUiMapping.ksKsiegowoscRBtn);
		
		return ksPortal.isSelected();
	}
	
	public boolean getStateOfKsDotacjeRBtn(){
		ksKsiegowosc = driver.findElement(MainPageUiMapping.ksKsiegowoscRBtn);
				
		return ksDotacje.isSelected();
	}
	
	public boolean getStateOfKsKsiegowoscRBtn(){
		ksKsiegowosc = driver.findElement(MainPageUiMapping.ksKsiegowoscRBtn);
		
		return ksKsiegowosc.isSelected();
	}
	
}
